package cn.xz.thread_demo;

import java.util.concurrent.*;

/**
 * newCachedThreadPool  带缓存的线程池，开启之后没有任务不会关闭，会一直等待
 *  newFixedThreadPool  指定大小线程池，指定核心线程数，且最大线程数与核心线程数一样大,开启之后没有任务不会关闭，会一直等待
 *  newSingleThreadExecutor  单例的线程池，核心线程数和最大线程数都是一，每次执行任务的线程都是同一个线程
 */
public class ThreadPool_demo {
    public static void main(String[] args) {
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        executorService.submit(()->{
//            System.out.println("缓存线程池来了....."+Thread.currentThread().getName());
//        });
//        ExecutorService executorService = Executors.newFixedThreadPool(5);
//        ExecutorService executorService = Executors.newSingleThreadExecutor();
//        for (int i = 0; i < 5; i++) {
//            executorService.submit(new Callable<String>() {
//                @Override
//                public String call() throws Exception {
//                    System.out.println("指定大小线程池来了...."+Thread.currentThread().getName());
//                    return "aa";
//                }
//            });
//        }

        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
        executorService.schedule(()->{
            System.out.println("只执行一次.....");
        },3,TimeUnit.SECONDS);
//        executorService.scheduleAtFixedRate(()->{
//            try {
//                System.out.println("调度线程来了...."+System.currentTimeMillis());
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        },3,6, TimeUnit.SECONDS);

//        executorService.scheduleWithFixedDelay(()->{
//            try {
//                System.out.println("调度线程来了...."+System.currentTimeMillis());
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        },3,1, TimeUnit.SECONDS);

    }
}
